package cn.shequren.communityPeople.pay.utils;

import android.content.Context;


import com.jz.community.basecomm.constant.AppConstants;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.json.JSONObject;

/**
 * Created by Tony.Fan on 2018/3/16 10:54
 */
public class WXPayUtil {

    private static WXPayUtil wxPayUtil = new WXPayUtil();

    public static WXPayUtil getInstance() {
        return wxPayUtil;
    }

    private IWXAPI api;

    public WXPayUtil(Context activity) {
        api = WXAPIFactory.createWXAPI(activity,AppConstants.WX_APP_ID);
    }

    public WXPayUtil() {
    }

    public void sendPayReq(String payInfo) {

        PayReq req = new PayReq();
        try {
            JSONObject json = new JSONObject(payInfo);
            req.appId = json.getString("appId");
            req.partnerId = json.getString("partnerId");
            req.prepayId = json.getString("prepayId");
            req.nonceStr = json.getString("nonceStr");
            req.timeStamp = json.getString("timeStamp");
            req.packageValue = json.getString("packageValue");
            req.sign = json.getString("sign");

        } catch (Exception e) {
            e.printStackTrace();
        }
        // 在支付之前，如果应用没有注册到微信，应该先调用IWXMsg.registerApp将应用注册到微信
        api.sendReq(req);
    }

    //检测是否安装微信
    public boolean isWxInstalledAndSurported() {
        return api.isWXAppInstalled() && api.isWXAppSupportAPI();
    }

}
